package cn.peach.user.service.impl;

import cn.peach.basic.exception.MyException;
import cn.peach.basic.service.impl.BaseServiceImpl;
import cn.peach.basic.util.MD5Utils;
import cn.peach.basic.util.StrUtils;
import cn.peach.user.constant.UserConstant;
import cn.peach.user.domain.Logininfo;
import cn.peach.user.domain.User;
import cn.peach.user.dto.UserDto;
import cn.peach.user.mapper.LogininfoMapper;
import cn.peach.user.mapper.UserMapper;
import cn.peach.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
     * 手机注册
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        // 非空校验四个参数   时间戳+手机号
        if (StringUtils.isEmpty(userDto.getPhone()) || StringUtils.isEmpty(userDto.getCode())
            || StringUtils.isEmpty(userDto.getPassword())
            || StringUtils.isEmpty(userDto.getComfirmPwd())){
            throw new MyException("参数不能为空");
        }
        // 再看手机号是否被注册
        // 获取user表中的phone 看有没有被注册
        User userPhone = userMapper.getPhone(userDto.getPhone());
        if (userPhone != null){
            throw new MyException("手机号已被注册，请直接登录");
        }
        // 还要判断两次输入的密码是否一致
        if (!userDto.getPassword().equals(userDto.getComfirmPwd())){
            throw new MyException("密码不一致，请重新输入");
        }
        // 校验码验证 redis获取校验码
        Object oCode = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX + userDto.getPhone());
        // 判断验证码为不为空
        if (oCode == null){
            throw new MyException("验证码已过期，请重新获取");
        }else {
            // 将上面获取的验证码与用户输入的验证码作对比  先把验证码分割出来
            // equalsIgnoreCase 这个方法区分大小写
            String redisCode = oCode.toString().split(":")[1];
            if (!redisCode.equalsIgnoreCase(userDto.getCode())){
                throw new MyException("验证码错误，请重新输入");
            }
        }
        /* 先把数据保存到logininfo  准备user数据和loginginfo数据*/
        // 2--to 存进
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        // 返回主键自增
        logininfoMapper.add(logininfo);
        
        user.setLogininfo_id(logininfo.getId());
        userMapper.add(user);
    }

    /**
     * 获取logininfo对象
     * @param user
     * @return
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 复制user中的数据到logininfo中
        BeanUtils.copyProperties(user,logininfo);
        // 设置类型1位普通用户
        logininfo.setType(1);
        // 1为启用状态
        logininfo.setDisable(1);
        return logininfo;
    }

    /**
     * 获取user对象
     * @param userDto
     * @return
     */
    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(1);
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 加密加盐  password加盐加密过后的密码
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }
}
